package org.come.Jpanel;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Line2D;
import java.awt.image.ImageObserver;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.*;

import cn.hutool.core.util.StrUtil;
import com.tool.role.RoleData;
import com.tool.tcp.Sprite;
import com.tool.tcp.SpriteFactory;
import com.updateNew.MyIsif;
import org.come.Frame.TestsmallmapJframe;
import org.come.bean.ImgZoom;
import org.come.bean.LoginResult;
import org.come.bean.NpcInfoBean;
import org.come.bean.PathPoint;
import org.come.entity.Goodstable;
import org.come.good.Flight;
import org.come.model.Gamemap;
import org.come.mouslisten.SystemMouslisten;
import org.come.until.*;

import com.tool.btn.FormsOnOffBtn;
import com.tool.image.ImageMixDeal;
import com.tool.tcpimg.UIUtils;


import static org.come.until.Util.mapmodel;

public class TestsmallmapJpanel extends JPanel {
    private long time;
    private int Dx = 0;
    private int Dy = 0;
    private String xy = "";
    public static Map<Integer, String> MapName;
    public List pathPoints = new ArrayList();
    public JLabel[] sp;
    public Sprite tcp;
    public static ImageIcon icony;
    private static JLabel labNoC;
    private static JLabel move;
    private static JLabel synpc;
    private static JLabel rwnpc;
    private static JLabel qbnpx;
    public JLabel m1207;
    private FormsOnOffBtn offBtn;
    public static List<String> npcfunction;

    //        小地图旗子
    private static List<JLabel> tps = new ArrayList<>();
    public static List<Goodstable> chaxunss;
    //        小地图旗子
    public int pathPointsx;
    public int pathPointsy;

    public TestsmallmapJpanel(TestsmallmapJframe testsmallmapJframe) throws IOException {
        this.setLayout(null);
        this.setBackground(UIUtils.Color_BACK);

        time = 0L;
        icony = new ImageIcon("skin/8100226/newbutton/CSD.png");
        MapName = new HashMap();
        this.tcp = SpriteFactory.VloadSprite("resource/mouse/ZD.tcp", null);
        if ("水墨UI".equals(MyIsif.getStyle())) {
            this.add((Component) (this.offBtn = new FormsOnOffBtn("skin/8100226/newbutton/xdtgbaj.png", 1, 22)));
        } else {
            this.add((Component) (this.offBtn = new FormsOnOffBtn("skin/8100226/newbutton/xdtgbajh.png", 1, 22)));
        }
        this.add((Component) (this.m1207 = new JLabel()));
        (TestsmallmapJpanel.qbnpx = new JLabel()).setOpaque(false);
        TestsmallmapJpanel.qbnpx.setBounds(50, Util.mapmodel.getMin_y() + 30, 15, 15);
        TestsmallmapJpanel.qbnpx.addMouseListener(new SystemMouslisten(31));
        TestsmallmapJpanel.qbnpx.addMouseListener(new SystemMouslisten(36));
        this.add((Component) TestsmallmapJpanel.qbnpx);

        (TestsmallmapJpanel.move = new JLabel()).setOpaque(false);
        TestsmallmapJpanel.move.setBounds(100, Util.mapmodel.getMin_y() + 30, 15, 15);
        TestsmallmapJpanel.move.addMouseListener(new SystemMouslisten(28));
        TestsmallmapJpanel.move.addMouseListener(new SystemMouslisten(33));
        this.add((Component) TestsmallmapJpanel.move);

        (TestsmallmapJpanel.labNoC = new JLabel()).setOpaque(false);
        TestsmallmapJpanel.labNoC.setBounds(100 + 50, Util.mapmodel.getMin_y() + 30, 15, 15);
        TestsmallmapJpanel.labNoC.addMouseListener(new SystemMouslisten(27));
        TestsmallmapJpanel.labNoC.addMouseListener(new SystemMouslisten(32));
        this.add((Component) TestsmallmapJpanel.labNoC);

        (TestsmallmapJpanel.rwnpc = new JLabel()).setOpaque(false);
        TestsmallmapJpanel.rwnpc.setBounds(100 + 50 * 2, Util.mapmodel.getMin_y() + 30, 15, 15);
        TestsmallmapJpanel.rwnpc.addMouseListener(new SystemMouslisten(29));
        TestsmallmapJpanel.rwnpc.addMouseListener(new SystemMouslisten(34));
        this.add((Component) TestsmallmapJpanel.rwnpc);

        (TestsmallmapJpanel.synpc = new JLabel()).setOpaque(false);
        TestsmallmapJpanel.synpc.setBounds(100 + 50 * 3, Util.mapmodel.getMin_y() + 30, 15, 15);
        TestsmallmapJpanel.synpc.addMouseListener(new SystemMouslisten(30));
        TestsmallmapJpanel.synpc.addMouseListener(new SystemMouslisten(35));
        this.add((Component) TestsmallmapJpanel.synpc);

        this.sp = new JLabel[5];
        int i = 0;
        while (i < this.sp.length) {
            (this.sp[i] = new JLabel()).setOpaque(false);
            if ("水墨UI".equals(MyIsif.getStyle())) {
                this.sp[i].setIcon((Icon) CutButtonImage.getImage("skin/8100226/newbutton/XDRXZK.png", 18, 18));
            } else {
                this.sp[i].setIcon((Icon) CutButtonImage.getImage("skin/8100226/newbutton/XDRXZKh.png", 18, 18));
            }
            this.add((Component) this.sp[i]);
            ++i;
        }
    }


    //        小地图旗子
    public void clearTps() {
        for (JLabel tp : tps) {
            this.remove(tp);
        }
        tps.clear();
        chaxunss = null;
    }
    //        小地图旗子

    ImageIcon icon = new ImageIcon("skin/8100226/newbutton/DWTB5.png");
    Font f5 = new Font("TimesRoman", Font.BOLD + Font.ITALIC, 14);

    Color color = new Color(32, 17, 10);
    Color color1 = new Color(151, 82, 37);
    Color color2 = new Color(103, 15, 8);

    @Override
    protected void paintComponent(Graphics g) {
//        FormsOnOffBtn offBtn = new FormsOnOffBtn("skin/8100226/button/xdtgbaj.png", 1, 22);
//        offBtn.setBounds((Util.mapmodel.getMin_x()) + 48, +6, 40, 25);
//        this.add(offBtn);
        TestsmallmapJpanel.qbnpx.setBounds(22, Util.mapmodel.getMin_y() + 41, 15, 15);
        TestsmallmapJpanel.move.setBounds(90, Util.mapmodel.getMin_y() + 41, 15, 15);
        TestsmallmapJpanel.labNoC.setBounds(90 + 68, Util.mapmodel.getMin_y() + 41, 15, 15);
        TestsmallmapJpanel.rwnpc.setBounds(90 + 68 * 2, Util.mapmodel.getMin_y() + 41, 15, 15);
        TestsmallmapJpanel.synpc.setBounds(90 + 68 * 3, Util.mapmodel.getMin_y() + 41, 15, 15);
        final LoginResult loginResult = RoleData.getRoleData().getLoginResult();
        this.time += 12L;

        super.paintComponent(g);
//        小地图旗子
        if (chaxunss == null) {

            chaxunss = GoodsListFromServerUntil.chaxunss(2011);
            if (chaxunss == null) {
                chaxunss = new ArrayList<>();
            }
            chaxunss.addAll(GoodsListFromServerUntil.chaxunss(2012));
            Long mapid = ImageMixDeal.userimg.getRoleShow().getMapid();
            JLabel tp;
            for (Goodstable goodstable : chaxunss) {
                String[] split = goodstable.getValue().split("\\|");
                for (int i = 0; i < split.length; i++) {
                    String[] strings = split[i].split(",");
                    if (strings[0].contains(String.valueOf(mapid))) {
                        tp = new JLabel();
                        tp.setBounds((int) ((double) (Integer.parseInt(strings[2]) / 20) / Util.mapmodel.getBili_x()) + 3,
                                (int) ((double) (Integer.parseInt(strings[3]) / 20) / Util.mapmodel.getBili_y()) + 18, 16, 21);
                        if (goodstable.getType() == 2011) {
                            tp.setIcon(CutButtonImage.getImage("skin/8100226/button/蓝旗子.png", 16, 21));
                        } else {
                            tp.setIcon(CutButtonImage.getImage("skin/8100226/button/黄旗子.png", 16, 21));
                        }
                        tp.addMouseListener(new MouseListener() {
                            @Override
                            public void mouseClicked(MouseEvent e) {
                                //system.out.println("mouseClicked");
                                Flight.Flightchess(goodstable, goodstable.getType());
                            }

                            @Override
                            public void mousePressed(MouseEvent e) {
                                //system.out.println("mousePressed");
                            }

                            @Override
                            public void mouseReleased(MouseEvent e) {

                            }

                            @Override
                            public void mouseEntered(MouseEvent e) {

                            }

                            @Override
                            public void mouseExited(MouseEvent e) {

                            }
                        });
                        tps.add(tp);
                    }
                }
            }
        }

        for (JLabel tp : tps) {
            this.add(tp);
        }
//        小地图旗子
        Util.mapmodel.getZoom().draw(g);
        this.m1207.setBounds(Util.mapmodel.getMin_x() + 4, 50, 78, Util.mapmodel.getMin_y() - 25);
        this.offBtn.setBounds(Util.mapmodel.getMin_x() + 48, 6, 40, 25);
        //       更新小地图方法
        g.drawImage(this.icon.getImage(), (int) ((double) (ImageMixDeal.userimg.getRoleShow().getX() / 20) / Util.mapmodel.getBili_x()) + -6, (int) ((double) (ImageMixDeal.userimg.getRoleShow().getY() / 20) / Util.mapmodel.getBili_y()) + 15, 20, 25, this);
        g.setColor(Color.RED);

        final Graphics2D g2 = (Graphics2D) g;

        g.setFont(this.f5);
        g.drawString(this.xy, this.Dx, this.Dy);
        g.setFont(UIUtils.TEXT_FONT);
        g.setFont(UIUtils.TEXT_FONT);
        final Gamemap game = (Gamemap) UserMessUntil.getAllmapbean().getAllMap().get(loginResult.getMapid() + "");
        final List<String> strings = SplitStringTool.splitString(game.getMapnpc());

        g.setColor(Color.white);
        g.drawString("全部", 20 + 22, mapmodel.getMin_y() + 53);
        this.sp[4].setBounds(20, mapmodel.getMin_y() + 40, 18, 18);
        g.drawString("传送", 88 + 22, mapmodel.getMin_y() + 53);
        this.sp[1].setBounds(88, mapmodel.getMin_y() + 40, 18, 18);
        g.drawString("普通", 88 + 22 + 68, mapmodel.getMin_y() + 53);
        this.sp[0].setBounds(88 + 68, mapmodel.getMin_y() + 40, 18, 18);
        g.drawString("任务", 88 + 22 + 68 * 2, mapmodel.getMin_y() + 53);
        this.sp[2].setBounds(88 + 68 * 2, mapmodel.getMin_y() + 40, 18, 18);
        g.drawString("商业", 88 + 22 + 68 * 3, mapmodel.getMin_y() + 53);
        this.sp[3].setBounds(88 + 68 * 3, mapmodel.getMin_y() + 40, 18, 18);


        int j = 0;
        try {
            while (j < strings.size()) {
                final NpcInfoBean infoBean = UserMessUntil.getnpc((String) strings.get(j));
                if (infoBean != null && !StrUtil.isBlank(infoBean.getNpctable().getTx())) {

                    TestsmallmapJpanel.MapName.put(Integer.valueOf(j), infoBean.getNpctable().getNpcname());
                    final int x1 = (int) ((double) (Integer.parseInt(infoBean.getNpctable().getTx()) / 20) / Util.mapmodel.getBili_x() + 25.0);
                    final int y1 = (int) ((double) (Integer.parseInt(infoBean.getNpctable().getTy()) / 20) / Util.mapmodel.getBili_x() + 25.0);

//                    普通npc
//                    if (TestsmallmapJpanel.labNoC.getIcon() != null && !infoBean.getNpctable().getNpctype().equals("2")) {
                    if (TestsmallmapJpanel.labNoC.getIcon() != null && "2".equals(infoBean.getNpctable().getTypexdt())) {
                        g.setColor(Color.green);
                        g.drawString((String) TestsmallmapJpanel.MapName.get(Integer.valueOf(j)), x1 - 40, y1 + 10);
                    } else {
                        g.setColor(Color.green);
                        g.drawString("", x1 - 25, y1 + 10);
                    }
//                    任务npc
                    if (TestsmallmapJpanel.rwnpc.getIcon() != null && "3".equals(infoBean.getNpctable().getTypexdt())) {
                        g.setColor(Color.orange);
                        g.drawString((String) TestsmallmapJpanel.MapName.get(Integer.valueOf(j)), x1 - 40, y1 + 10);
                    } else {
                        g.setColor(Color.orange);
                        g.drawString("", x1 - 25, y1 + 10);
                    }
//                    商业npc
                    if (TestsmallmapJpanel.synpc.getIcon() != null && "4".equals(infoBean.getNpctable().getTypexdt())) {
                        g.setColor(Color.cyan);
                        g.drawString((String) TestsmallmapJpanel.MapName.get(Integer.valueOf(j)), x1 - 40, y1 + 10);
                    } else {
                        g.setColor(Color.cyan);
                        g.drawString("", x1 - 25, y1 + 10);
                    }
//                    传送npc
                    if (TestsmallmapJpanel.move.getIcon() != null && "1".equals(infoBean.getNpctable().getTypexdt())) {
                        g.drawImage(TestsmallmapJpanel.icony.getImage(), x1 - 30, y1 - 5, (ImageObserver) this);
                    } else {
                        g.drawImage(null, x1 - 25, y1 + 10, (ImageObserver) this);
                    }
                }

                ++j;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        g.setFont(this.f5);
//        Graphics2D g2 = (Graphics2D)g;
        g.setColor(Color.RED);
        if (this.pathPoints.size() > 1) {
            for (int i = 0; i < this.pathPoints.size() - 1; ++i) {
                Stroke dash = new BasicStroke(5.0F, 1, 1, 3.5F, new float[]{2.5F, 10.0F}, 0.0F);
                g2.setStroke(dash);
                //       更新小地图方法
                g2.draw(new Line2D.Double((double) ((int) ((double) (((PathPoint) this.pathPoints.get(i)).getX() / 20) / Util.mapmodel.getBili_x() + 7.0D)), (double) ((int) ((double) (((PathPoint) this.pathPoints.get(i)).getY() / 20) / Util.mapmodel.getBili_x() + 40.0D)), (double) ((int) ((double) (((PathPoint) this.pathPoints.get(i + 1)).getX() / 20) / Util.mapmodel.getBili_x() + 7.0D)), (double) ((int) ((double) (((PathPoint) this.pathPoints.get(i + 1)).getY() / 20) / Util.mapmodel.getBili_x() + 40.0D))));
//                g2.draw(new Line2D.Double((double)((int)((double)(((PathPoint)this.pathPoints.get(i)).getX() / 20) / Util.mapmodel.getBili_x() + 25.0D)), (double)((int)((double)(((PathPoint)this.pathPoints.get(i)).getY() / 20) / Util.mapmodel.getBili_x() + 25.0D)), (double)((int)((double)(((PathPoint)this.pathPoints.get(i + 1)).getX() / 20) / Util.mapmodel.getBili_x() + 25.0D)), (double)((int)((double)(((PathPoint)this.pathPoints.get(i + 1)).getY() / 20) / Util.mapmodel.getBili_x() + 25.0D))));
            }

            tcp.updateToTime(ImageMixDeal.userimg.getTime(), 0);
            tcp.draw(g, ((int) ((double) (((PathPoint) this.pathPoints.get(this.pathPoints.size() - 1)).getX() / 20) / mapmodel.getBili_x() + 10.0D) - 28), ((int) ((double) (((PathPoint) this.pathPoints.get(this.pathPoints.size() - 1)).getY() / 20) / mapmodel.getBili_x() + 20.0D) - 10));
//            g2.dispose();
        }

    }


    public List<String> getNpcfunction() {
        return TestsmallmapJpanel.npcfunction;
    }

    public int getPathPointsx() {
        return this.pathPointsx;
    }

    public void setPathPointsx(final int pathPointsx) {
        this.pathPointsx = pathPointsx;
    }

    public int getPathPointsy() {
        return this.pathPointsy;
    }

    public void setPathPointsy(final int pathPointsy) {
        this.pathPointsy = pathPointsy;
    }

    public String getXy() {
        return xy;
    }

    public void setXy(String xy) {
        this.xy = xy;
    }

    public int getDx() {
        return Dx;
    }

    public void setDx(int dx) {
        Dx = dx;
    }

    public int getDy() {
        return Dy;
    }

    public void setDy(int dy) {
        Dy = dy;
    }

    public FormsOnOffBtn getOffBtn() {
        return offBtn;
    }

    public void setOffBtn(FormsOnOffBtn offBtn) {
        this.offBtn = offBtn;
    }

    public static JLabel getLabNoC() {
        return labNoC;
    }

    public static void setLabNoC(JLabel labNoC) {
        TestsmallmapJpanel.labNoC = labNoC;
    }

    public static JLabel getMove() {
        return move;
    }

    public static void setMove(JLabel move) {
        TestsmallmapJpanel.move = move;
    }


    public static JLabel getQbnpx() {
        return qbnpx;
    }

    public static void setQbnpx(JLabel qbnpx) {
        TestsmallmapJpanel.qbnpx = qbnpx;
    }

    public static JLabel getRwnpc() {
        return rwnpc;
    }

    public static void setRwnpc(JLabel rwnpc) {
        TestsmallmapJpanel.rwnpc = rwnpc;
    }


    public static JLabel getSynpc() {
        return synpc;
    }

    public static void setSynpc(JLabel synpc) {
        TestsmallmapJpanel.synpc = synpc;
    }

}
